//
//  CityView.m
//  City
//
//  Created by 红沙尘 on 2018/2/28.
//  Copyright © 2018年 红沙尘. All rights reserved.
//

#import "CityView.h"
#import "AreaModel.h"
#import "CityModel.h"
#import "ProvinceModel.h"

@interface CityView ()<UIPickerViewDelegate,UIPickerViewDataSource> {
    
    NSMutableDictionary *_areaDic;
    __weak IBOutlet UIPickerView *_pickerView;
}

@property (nonatomic, strong) NSMutableArray *provinceArr;

@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *area;

@end

@implementation CityView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self loading];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self loading];
    }
    
    return self;
}

- (void)loading {
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(globalQueue, ^{
        [self prepareData];
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^{
            [self uiConfig];
        });
    });
}

- (void)prepareData {
    
    _provinceArr = [[NSMutableArray alloc] init];
    
    //area.plist是字典
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
    _areaDic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    
    for (NSString *key in _areaDic.allKeys) {
        NSMutableArray *arrM = [_areaDic[key] mutableCopy];
        
        NSDictionary *dict = [arrM firstObject];
        if ([dict[@"name"] isEqualToString:@"市辖区"]) {
            [arrM removeObjectAtIndex:0];
            _areaDic[key] = arrM;
        }
    }
    
    //city.plist是数组
    NSString *plist = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
    NSMutableArray *dataCity = [[NSMutableArray alloc] initWithContentsOfFile:plist];
    
    for (NSDictionary *dic in dataCity) {
        ProvinceModel *model  = [[ProvinceModel alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        model.citiesArr = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dic in model.cities) {
            CityModel *cityModel = [[CityModel alloc] init];
            [cityModel setValuesForKeysWithDictionary:dic];
            [model.citiesArr addObject:cityModel];
        }
        [_provinceArr addObject:model];
    }
    
    [self selectFirst:0 second:0 third:0];
}

- (void)uiConfig {
    
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_pickerView selectRow:0 inComponent:0 animated:YES];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (0 == component) {
        return _provinceArr.count;
        
    } else if(1 == component) {
        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
        ProvinceModel *model = _provinceArr[rowProvince];
        return model.citiesArr.count;
        
    } else {
        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
        NSInteger rowCity = [pickerView selectedRowInComponent:1];
        ProvinceModel *model = _provinceArr[rowProvince];
        CityModel *cityModel = model.citiesArr[rowCity];
        NSString *str = [cityModel.code description];
        
        NSMutableArray *arrM  = [_areaDic[str] mutableCopy];
        return arrM.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
    
    if (0 == component) {
        
        ProvinceModel *model = _provinceArr[row];
        return model.name;
        
    } else if(1==component) {
        
        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
        ProvinceModel *model = _provinceArr[rowProvince];
        CityModel *cityModel = model.citiesArr[row];
        return cityModel.name;
        
    } else {
        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
        NSInteger rowCity = [pickerView selectedRowInComponent:1];
        ProvinceModel *model = _provinceArr[rowProvince];
        CityModel *cityModel = model.citiesArr[rowCity];
        NSString *str = [cityModel.code description];
        
        NSMutableArray *arrM  = [_areaDic[str] mutableCopy];
        AreaModel *areaModel = [[AreaModel alloc] init];
        [areaModel setValuesForKeysWithDictionary:arrM[row]];
        return areaModel.name;
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {
    
    if (0 == component) {
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        
    } if(1 == component) {
        [pickerView reloadComponent:2];
    }
    
    NSInteger selectOne = [pickerView selectedRowInComponent:0];
    NSInteger selectTwo = [pickerView selectedRowInComponent:1];
    NSInteger selectThree = [pickerView selectedRowInComponent:2];
    
    [self selectFirst:selectOne second:selectTwo third:selectThree];
}

- (void)selectFirst:(NSInteger)firtst second:(NSInteger)second third:(NSInteger)third {
    
    ProvinceModel *model = _provinceArr[firtst%_provinceArr.count];
    CityModel *cityModel = model.citiesArr[second%model.citiesArr.count];
    NSString *str = [cityModel.code description];
    NSArray *arr = _areaDic[str];
    
    AreaModel *areaModel = [[AreaModel alloc] init];
    [areaModel setValuesForKeysWithDictionary:arr[third%arr.count]];
    
    NSString *address = [NSString stringWithFormat:@"%@%@%@",model.name,cityModel.name,areaModel.name];
    NSLog(@"address = %@",address);
}

- (IBAction)cancel {
    [self removeFromSuperview];
}

- (IBAction)done {
    
    if ([self.delegate respondsToSelector:@selector(cityViewDidSelectedFinished:)]) {
        [self.delegate cityViewDidSelectedFinished:self];
    }
    
    [self removeFromSuperview];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end

